package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class BibliotecaApp {

    Menu menu;

    public BibliotecaApp(Menu menu) {
        this.menu = menu;
    }

    public static void main(String[] args) throws IOException {

        IOManager ioManager = new IOManager(System.out, new BufferedReader(new InputStreamReader(System.in)));
        Library library = createLibrary();
        Map<String, LibraryAction> options = configureOptions(ioManager, library);
        Menu menu = new Menu(ioManager, "Welcome to Biblioteca!", options);

        new BibliotecaApp(menu).go();

    }

    private static Library createLibrary() {
        List<LibraryBook> bookList = new ArrayList<LibraryBook>();
        bookList.add(new LibraryBook("The Wizard of Oz"));
        return new Library(bookList);
    }

    private static Map<String, LibraryAction> configureOptions(IOManager ioManager, Library library) {
        Map<String, LibraryAction> options = new TreeMap<String, LibraryAction>();

        options.put("1", new BookLister(library, ioManager));
        options.put("2", new CheckOuter(library, ioManager));
        options.put("3", new CheckInner(library, ioManager));
        options.put("9", new Quitter());
        return options;
    }

    public void go() throws IOException {
        menu.welcome();
        menu.menu();
    }
}
